import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.Buffer;

import lejos.nxt.addon.NXTLineLeader.Command;
import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommException;
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTCommInputStream;
import lejos.pc.comm.NXTInfo;

public class DebugGUI {
	public static void main(String[] args) {

		Frame frm = new Frame("Bluetooth debug GUI");
		Label lbl = new Label("Bluetooth debug GUI");
		frm.add(lbl);
		frm.setSize(350, 200);
		frm.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		Panel p = new Panel();
		Panel p1 = new Panel();

		Label jCompass = new Label("Compass:");
		TextField lCompass = new TextField(20);

		Label jLightsensor = new Label("Lightsensor");
		TextField lLightsensor = new TextField(20);

		Label jInfrared = new Label("Infrared");
		TextField lInfrared = new TextField(20);

		Label jUltrasonic = new Label("Ultasonic");
		TextField lUltrasonic = new TextField(20);

		p.setLayout(new GridLayout(3, 1));

		p.add(jCompass);
		p.add(lCompass);
		p.add(jLightsensor);
		p.add(lLightsensor);
		p.add(jInfrared);
		p.add(lInfrared);
		p.add(jUltrasonic);
		p.add(lUltrasonic);

		p1.add(p);
		frm.add(p1, BorderLayout.NORTH);

		NXTInfo info = new NXTInfo(1, "FlipperButton", "0016530950E3");
		NXTComm comm = null;

		try {
			comm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
			comm.open(info);
		} catch (NXTCommException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		InputStream in = comm.getInputStream();
		DataInputStream din = new DataInputStream(in);

		int maxFields = 5;
		int field = 1;
		boolean running = true;
		while (running) {
			try {
				int n1 = din.read();
				int n2 = din.read();
				int tot = ((n1 << 8) + n2);
				if (field == 1)
					System.out.println("Tick: " + tot);
				else if (field == 2)
					System.out.println("Compass: " + tot);
				else if (field == 3)
					System.out.println("IR: " + tot);
				else if (field == 4)
					System.out.println("light: " + tot);
				else if (field == 5) {
					System.out.println("ultra: " + tot);
					field = 0;
				}
				field++;

			} catch (IOException e) {
				// TODO Auto-generated catch block
				running = false;
				e.printStackTrace();
			}
		}

		// try {
		// comm.close();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		// System.out.println("done");
	}
}
